package gui;

import gui.utils.UnsubscribeComboBoxModel;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

public class PanelTopicManagement extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	private javax.swing.JButton buttonSubscribe;
	private javax.swing.JButton buttonUnsubscribe;
	private javax.swing.JComboBox comboBoxTopicToUnsubscribe;
	private javax.swing.JLabel labelDescriptionSubscribe;
	private javax.swing.JLabel labelDescriptionUnsubscribe;
	private javax.swing.JScrollPane scrollPaneTopicsList;
	private javax.swing.JTextArea textAreaTopicsList;
	private javax.swing.JTextField textFieldNewTopic;

	public PanelTopicManagement() {
		initComponents();
	}

	private void buttonSubscribeActionPerformed(java.awt.event.ActionEvent evt) {
		boolean success = false;
		try {
			success = ClientGuiLauncher.clientFrame.client
					.subscribe(textFieldNewTopic.getText());
		} catch (Exception e) {
			Logger.getLogger(PanelTopicManagement.class.getName()).log(
					Level.SEVERE, null, e);
		}
		if (!success) {
			JOptionPane.showMessageDialog(null, "Already subscribed to topic "
					+ textFieldNewTopic.getText(), "Warning",
					JOptionPane.WARNING_MESSAGE);
		} else {
			updateComboBoxesModel();
			setTopicsList();
		}
	}

	private void buttonUnsubscribeActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			ClientGuiLauncher.clientFrame.client
					.unsubscribe(comboBoxTopicToUnsubscribe.getSelectedItem()
							.toString());
		} catch (Exception e) {
			Logger.getLogger(PanelTopicManagement.class.getName()).log(
					Level.SEVERE, null, e);
		}
		updateComboBoxesModel();
		setTopicsList();
	}

	private void comboBoxTopicToUnsubscribeActionPerformed(
			java.awt.event.ActionEvent evt) {
		if (comboBoxTopicToUnsubscribe.getSelectedItem().equals(""))
			buttonUnsubscribe.setEnabled(false);
		else
			buttonUnsubscribe.setEnabled(true);
	}

	protected void disableAll() {
		textFieldNewTopic.setEditable(false);
		textFieldNewTopic.setText("");
		comboBoxTopicToUnsubscribe.setModel(new UnsubscribeComboBoxModel(
				ClientGuiLauncher.clientFrame.client.getSubscribedTopics()));
		buttonSubscribe.setEnabled(false);
		buttonUnsubscribe.setEnabled(false);
		textAreaTopicsList.setText("");
	}

	private void initComponents() {

		labelDescriptionSubscribe = new javax.swing.JLabel();
		textFieldNewTopic = new javax.swing.JTextField();
		buttonSubscribe = new javax.swing.JButton();
		scrollPaneTopicsList = new javax.swing.JScrollPane();
		textAreaTopicsList = new javax.swing.JTextArea();
		labelDescriptionUnsubscribe = new javax.swing.JLabel();
		comboBoxTopicToUnsubscribe = new javax.swing.JComboBox();
		buttonUnsubscribe = new javax.swing.JButton();

		labelDescriptionSubscribe
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		labelDescriptionSubscribe
				.setText("Post a new topic to which you want to subscribe");

		textFieldNewTopic.setEditable(false);
		textFieldNewTopic.addKeyListener(new java.awt.event.KeyAdapter() {
			@Override
			public void keyReleased(java.awt.event.KeyEvent evt) {
				textFieldNewTopicKeyReleased(evt);
			}
		});

		buttonSubscribe.setText("Subscribe");
		buttonSubscribe.setEnabled(false);
		buttonSubscribe.setMargin(new java.awt.Insets(2, 5, 2, 5));
		buttonSubscribe.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonSubscribeActionPerformed(evt);
			}
		});

		textAreaTopicsList.setColumns(20);
		textAreaTopicsList.setEditable(false);
		textAreaTopicsList.setRows(5);
		scrollPaneTopicsList.setViewportView(textAreaTopicsList);

		labelDescriptionUnsubscribe
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		labelDescriptionUnsubscribe
				.setText("Select the topic to which you want to unsubscribe");

		comboBoxTopicToUnsubscribe.setEditable(false);
		comboBoxTopicToUnsubscribe
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						comboBoxTopicToUnsubscribeActionPerformed(evt);
					}
				});

		buttonUnsubscribe.setText("Unsubscribe");
		buttonUnsubscribe.setEnabled(false);
		buttonUnsubscribe.setMargin(new java.awt.Insets(2, 5, 2, 5));
		buttonUnsubscribe
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						buttonUnsubscribeActionPerformed(evt);
					}
				});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														scrollPaneTopicsList,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														281, Short.MAX_VALUE)
												.addComponent(
														labelDescriptionSubscribe,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														281, Short.MAX_VALUE)
												.addGroup(
														javax.swing.GroupLayout.Alignment.LEADING,
														layout.createSequentialGroup()
																.addComponent(
																		textFieldNewTopic,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		185,
																		Short.MAX_VALUE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		buttonSubscribe,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		110,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		comboBoxTopicToUnsubscribe,
																		0,
																		185,
																		Short.MAX_VALUE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		buttonUnsubscribe,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		110,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addComponent(
														labelDescriptionUnsubscribe,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(labelDescriptionSubscribe)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														textFieldNewTopic,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														buttonSubscribe,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(scrollPaneTopicsList,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										213, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(labelDescriptionUnsubscribe)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														buttonUnsubscribe,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														comboBoxTopicToUnsubscribe,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));
	}

	protected void setInitialComponentsState() {
		textFieldNewTopic.setEditable(true);
		textFieldNewTopic.setText("");
		setTopicsList();
		buttonSubscribe.setEnabled(false);
		comboBoxTopicToUnsubscribe.setEditable(true);
		comboBoxTopicToUnsubscribe.setModel(new UnsubscribeComboBoxModel(
				ClientGuiLauncher.clientFrame.client.getSubscribedTopics()));

	}

	private void setTopicsList() {
		textAreaTopicsList.setText("");
		for (String topic : ClientGuiLauncher.clientFrame.client
				.getSubscribedTopics())
			textAreaTopicsList.append(topic + "\n");
	}

	private void textFieldNewTopicKeyReleased(java.awt.event.KeyEvent evt) {
		if (textFieldNewTopic.getText().equals(""))
			buttonSubscribe.setEnabled(false);
		else
			buttonSubscribe.setEnabled(true);
	}

	private void updateComboBoxesModel() {
		comboBoxTopicToUnsubscribe.setModel(new UnsubscribeComboBoxModel(
				ClientGuiLauncher.clientFrame.client.getSubscribedTopics()));
		ClientGuiLauncher.clientFrame
				.getPanelPublish()
				.getComboBoxTopicsList()
				.setModel(
						new UnsubscribeComboBoxModel(
								ClientGuiLauncher.clientFrame.client
										.getSubscribedTopics()));
		ClientGuiLauncher.clientFrame
				.getPanelMultiPublish()
				.getComboBoxTopicsList()
				.setModel(
						new UnsubscribeComboBoxModel(
								ClientGuiLauncher.clientFrame.client
										.getSubscribedTopics()));
	}
}
